package com.example.practice.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.practice.R;
import com.example.practice.utils.SharedpreferencesUtils;

public class editPasswordActivity extends AppCompatActivity {
    private TextView tv_title;
    private TextView tv_back;
    private RelativeLayout rl_title_bar;
    private Button btn_save_yl;
    private EditText et_past_pwd_yl, et_new_pwd_yl,et_new_pwd_again_yl;
    private String pastPwd_yl, newPwd_yl, newPwdAgain_yl;
    private String userName_yl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_password);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        initView();
        userName_yl= SharedpreferencesUtils.readLoginUserName(this);
    }

    private void initView() {
        tv_title=findViewById(R.id.tv_title);
        tv_title.setText("修改密码");
        tv_back=findViewById(R.id.tv_back);
        rl_title_bar=findViewById(R.id.rl_bar);
        et_past_pwd_yl=findViewById(R.id.et_past_pwd_yl);
        et_new_pwd_yl=findViewById(R.id.et_new_pwd_yl);
        et_new_pwd_again_yl=findViewById(R.id.et_new_pwd_again_yl);
        btn_save_yl=findViewById(R.id.btn_save_yl);

        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editPasswordActivity.this.finish();
            }
        });

        btn_save_yl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                click();
            }
        });
    }

    /**
     * 获取用户输入的字符串
     */
    private void getEditString(){
        pastPwd_yl=et_past_pwd_yl.getText().toString().trim();
        newPwd_yl=et_new_pwd_yl.getText().toString().trim();
        newPwdAgain_yl=et_new_pwd_again_yl.getText().toString().trim();
    }

    /**
     * 根据用户名从SP中读取用户密码
     */
    private String readPwd(){
        SharedPreferences sp = getSharedPreferences("loginInfo",MODE_PRIVATE);
        String spPwd = sp.getString(userName_yl,"");
        return spPwd;
    }

    /**
     * 修改用户保存在SP中的密码
     */
    private void editPwd(String newPwd){
        SharedPreferences sp=getSharedPreferences("loginInfo",MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putString(userName_yl, newPwd);
        editor.commit();
    }

    private void click(){
        getEditString();
        if(TextUtils.isEmpty(pastPwd_yl)){
            Toast.makeText(editPasswordActivity.this,"请输入旧密码",Toast.LENGTH_SHORT).show();
            return;
        }
        else if(TextUtils.isEmpty(newPwd_yl)) {
            Toast.makeText(editPasswordActivity.this, "请输入新密码", Toast.LENGTH_SHORT).show();
            return;
        }
        else if(TextUtils.isEmpty(newPwdAgain_yl)){
            Toast.makeText(editPasswordActivity.this,"请在此输入新密码", Toast.LENGTH_SHORT).show();
            return;
        }
        else if(!newPwd_yl.equals(newPwdAgain_yl)){
            Toast.makeText(editPasswordActivity.this,"两次输入的密码不一致", Toast.LENGTH_SHORT).show();
            return;
        }
        else if(!pastPwd_yl.equals(readPwd())){
            Toast.makeText(editPasswordActivity.this,"输入的密码与原始密码不一致",Toast.LENGTH_SHORT).show();
            return;
        }
        else if(newPwd_yl.equals(readPwd())){
            Toast.makeText(editPasswordActivity.this,"新密码与旧密码不能一样", Toast.LENGTH_SHORT).show();
            return;
        }
        else {
            Toast.makeText(editPasswordActivity.this,"新密码设置成功",Toast.LENGTH_SHORT).show();
            editPwd(newPwd_yl);
            Intent intent = new Intent(editPasswordActivity.this, loginActivity_yl.class);
            startActivity(intent);
            settingActivity_yl.instance.finish();
            editPasswordActivity.this.finish();
        }
    }
}